<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
		function User(name, password) {
			this.name = name;
			this.password = password;
		}

		function Pet(nickName, gender, owner) {
			console.log(nickName, gender, owner);
			this.nickName = nickName;
			this.gender = gender;
			this.status = 80;
			this.owner = owner;
			// 获取当前时间毫秒数
			this.lastEatTime = Date.now();
		}
		Pet.prototype.eat = function() {
			var nowTime = Date.now();
			console.log(nowTime);
			if (nowTime - this.lastEatTime > 5000) {
				this.lastEatTime = nowTime;
				this.status += 20;
				console.log(`${this.nickName}开始吃饭啦，吃过后的精力值是${this.status}`);
			} else {
				console.log(`主人我吃的太频繁了，吃不下！`);
			}
		}
		Pet.prototype.isLive = function() {
			if (this.status < 0) {
				alert(`主人，我的临终遗言是：我被你玩死了，游戏结束，你别玩这个游戏了，去打LOL吧`);
				return false;
			}
			return true;
		}

		function Pig() {
			this.type = "香猪";
			Pet.apply(this, arguments);
		}
		Pig.prototype = new Pet();
		Pig.prototype.constructor = Pig;
		Pig.prototype.play = function() {
			this.status -= 15;
			console.log(`主人，${this.type + this.nickName}出去游泳啦~ 
			现在精力剩余是：${this.status}`);
		}

		function Cat() {
			this.type = "猫猫";
			Pet.apply(this, arguments);
		}
		Cat.prototype = new Pet();
		Cat.prototype.constructor = Cat;
		Cat.prototype.play = function() {
			this.status -= 5;
			console.log(`主人，${this.type + this.nickName}出去上树啦~ 
			现在精力剩余是：${this.status}`);
		}

		function Rabbit() {
			this.type = "兔子";
			Pet.apply(this, arguments);
		}
		Rabbit.prototype = new Pet();
		Rabbit.prototype.constructor = Rabbit;
		Rabbit.prototype.play = function() {
			this.status -= 25;
			console.log(`主人，${this.type + this.nickName}去参加跑步比赛啦~ 
			现在精力剩余是：${this.status}`);
		}

		var playGame = {
			loginUsers: [{
				name: "aaa",
				password: "123456"
			}],
			loginedName: null,
			isDead:false,
			petList: [],
			dangerWords: ["毛泽东", "周恩来", "江泽民"],
			start: function() {
				do {
					var mainInput = prompt("请输入操作序号：1.注册2.登录3.退出", 2)
				} while (!/^[123]$/.test(mainInput))

				switch (mainInput) {
					case "1":
						this.register();
						break;
					case "2":
						this.login();
						break;
					default:
						console.log("已退出");
						break;
				}
			},
			register: function() {
				console.log("注册");
				do {
					var regName = prompt("请输入要注册的用户名3-8位")
					if (!/^[\u4E00-\u9FA5A-Za-z][\u4E00-\u9FA5A-Za-z0-9_-]{2,7}$/.test(regName)) {
						console.log("输入不合法");
						continue;
					}
					if (this.haveSameName(regName)) {
						console.log("已重名");
						continue;
					}
					if (this.haveDangerWords(regName)) {
						console.log("不能包含敏感词！");
						continue;
					}
					break;
				} while (true)

				do {
					var pwd1 = prompt("请设置密码，至少6位数");
					if (!/^\d{6,}$/.test(pwd1)) {
						console.log("密码至少6位数！");
						continue;
					}
					break;
				} while (true)

				do {
					var pwd2 = prompt("请设置重复密码，至少6位数");
					if (!/^\d{6,}$/.test(pwd2)) {
						console.log("密码至少6位数！");
						continue;
					}
					if (pwd1 != pwd2) {
						console.log("两次输入的密码不一致，请重新输入！");
						continue;
					}
					break;
				} while (true)

				this.loginUsers.push(new User(regName, pwd2))
				console.table(this.loginUsers);
				this.start();
			},
			login: function() {
				console.log("登录");
				do {
					var loginName = prompt("请输入要登录的用户名3-8位", "aaa")
					if (!/^[\u4E00-\u9FA5A-Za-z][\u4E00-\u9FA5A-Za-z0-9_-]{2,7}$/.test(loginName)) {
						console.log("输入不合法");
						continue;
					}
					// 登录验证用户名存在
					if (!this.haveSameName(loginName)) {
						console.log("没有此用户");
						continue;
					}

					break;
				} while (true)

				do {
					var pwd1 = prompt("请输入密码，至少6位数", "123456");
					if (!/^\d{6,}$/.test(pwd1)) {
						console.log("密码至少6位数！");
						continue;
					}
					// 登录密码是否在数组中
					if (!this.haveRightPassword(loginName, pwd1)) {
						console.log("密码错误！");
						continue;
					}
					break;
				} while (true)


				// 记录登录者信息
				this.loginedName = loginName;
				// 游玩选项菜单
				this.playItem();
			},
			playItem: function() {
				do {
					var select = prompt("请选择要做的操作：\n1.购买宠物 \n2.查看宠物 \n3.注销", 2);

				} while (!/^[123]$/.test(select))

				switch (select) {
					case "1":
						this.buyPet();
						break;
					case "2":
						this.seePet();
						console.log("查看宠物");
						this.actionChoose();
						break;
					default:
						this.loginedName = null;
						console.log("注销loginedName:", this.loginedName);
						this.start();
						break;
				}
			},
			actionChoose: function() {
				do {
					var act = prompt("请输入动作数字：\n1.喂它 \n2.跟它玩 \n3.返回上一级", 2);
				} while (!/^[123]$/)

				switch (act) {
					case "1":
						console.log("喂它");
						this.getOwenrPets().forEach(function(item) {
							item.eat();
						});
						this.actionChoose();
						break;
					case "2":
						console.log("跟它玩");
						var that = this;
						this.getOwenrPets().forEach(function(item) {
							item.play();
							if(!item.isLive()){
								that.isDead = true;
								console.log("GAME OVER");
								window.close();
							}
						});
						console.log(this.isDead);
						if(!this.isDead) {
							this.actionChoose();
						}
						break;
					default:
						console.log("返回上一级");
						this.playItem();
						break;
				}
			},
			seePet: function() {
				var that = this;

				if (!this.havePet(that.loginedName)) {
					console.log("你还没有任何宠物，请先去购买!");
					that.playItem();
				}
				this.petList.forEach(function(item) {
					if (item.owner == that.loginedName) {
						console.log(`主人，我的名字是：${item.nickName},性别是：${item.gender == "1"?"男生":"女生"}，
						状态是：${item.status},主人叫：${item.owner}`);
					}
				})
			},
			havePet: function(username) {
				return this.petList.some(function(item) {
					return item.owner == username;
				})
			},
			buyPet: function() {
				console.log("购买宠物");
				do {
					var choosePet = prompt("请选择宠物类型：1:香猪 2:猫咪 3:兔子", 3);
				} while (!/^[123]$/)

				do {
					var nickName = prompt("请输入宠物昵称", "秀儿是你吗")
				} while (!/^[a-zA-Z\u4e00-\u9fa5]+$/)

				do {
					var chooseGender = prompt("请选择宠物性别：1:男娃 2:女娃", 1);
				} while (!/^[12]$/)
				var myPet = null;
				switch (choosePet) {
					case "1":
						myPet = new Pig(nickName, chooseGender, this.loginedName)
						break;
					case "2":
						myPet = new Cat(nickName, chooseGender, this.loginedName)
						break;
					default:
						myPet = new Rabbit(nickName, chooseGender, this.loginedName)
						break;
				}
				this.petList.push(myPet)
				// 游玩选项菜单
				this.playItem();
			},
			// 得到所有属于我的宠物
			getOwenrPets: function() {
				var that = this;
				return this.petList.filter(function(item) {
					return item.owner == that.loginedName;
				})
			},
			haveSameName: function(regName) {
				return this.loginUsers.some(function(item) {
					return item.name == regName
				})
			},
			haveRightPassword: function(loginName, password) {
				return this.loginUsers.some(function(item) {
					return item.name == loginName && item.password == password
				})
			},
			haveDangerWords: function(words) {
				return this.dangerWords.includes(words);
			}
		}
		playGame.start();
	</script>
</html>
